package com.avic.rong.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.wedgit.DeviceInfoUtil;
import com.avic.rong.wedgit.StringUtil;


/**
 * dialog弹出框管理
 * created by xtj on 2017/12/6 16:10.
 */

public class DialogCustom {

    public static Dialog showSingleDialog(Context context, String title, String des, String middle, CustomDialogSingle dialogCallBack) {
        return showDialog(context, title, des, middle, dialogCallBack);
    }
    public static Dialog showSingleNoTitleDialog(Context context, String des, String middle, CustomDialogSingle dialogCallBack) {
        return showDialog(context, null, des, middle, dialogCallBack);
    }
    public static Dialog showSingleNoDesDialog(Context context, String title, String middle, CustomDialogSingle dialogCallBack) {
        return showDialog(context, title, null, middle, dialogCallBack);
    }

    public static Dialog showSingleDialog(Context context, int titleId, int desId, int middleId, CustomDialogSingle dialogCallBack) {
        return showDialog(context, context.getResources().getString(titleId), context.getResources().getString(desId), context.getResources().getString(middleId), dialogCallBack);
    }
    public static Dialog showSingleNoTitleDialog(Context context, int desId, int middleId, CustomDialogSingle dialogCallBack) {
        return showDialog(context, null, context.getResources().getString(desId), context.getResources().getString(middleId), dialogCallBack);
    }
    public static Dialog showSingleNoDesDialog(Context context, int titleId, int middleId, CustomDialogSingle dialogCallBack) {
        return showDialog(context,  context.getResources().getString(titleId),null, context.getResources().getString(middleId), dialogCallBack);
    }

    public static Dialog showDoubleDialog(Context context, String title, String des, String left, String right, CustomDialogDouble dialogCallBack) {
        return showDialog(context, title, des, left, right,dialogCallBack);
    }

    /**
     * @param context
     * @param title          标题
     * @param des            内容
     * @param left           左侧按钮
     * @param right          右侧按钮
     * @param dialogCallBack 点击回调
     * @return
     */
    protected static Dialog showDialog(Context context, String title, String des, String left, String right, final CustomDialogDouble dialogCallBack) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null);
        dialog.setContentView(view);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        if (!StringUtil.isEmpty(title)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        TextView tvDes = view.findViewById(R.id.tv_des);
        if (!StringUtil.isEmpty(des)) {
            tvDes.setVisibility(View.VISIBLE);
            tvDes.setText(des);
        }

        TextView tvLeft = view.findViewById(R.id.tv_left);
        if (!StringUtil.isEmpty(left)) {
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(left);
            tvLeft.setOnClickListener(new OnClickLimitListener() {
                @Override
                public void onClickLimit(View v) {
                    dialog.dismiss();
                    if (dialogCallBack != null) {
                        dialogCallBack.leftButtonClicked();
                    }

                }
            });
        }
        TextView tvRight = view.findViewById(R.id.tv_right);
        View viewLine = view.findViewById(R.id.view_line);
        if (!StringUtil.isEmpty(right)) {
            viewLine.setVisibility(View.VISIBLE);
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(right);
            tvRight.setOnClickListener(new OnClickLimitListener() {
                @Override
                public void onClickLimit(View v) {
                    dialog.dismiss();
                    if (dialogCallBack != null) {
                        dialogCallBack.rightButtonClicked();
                    }
                }
            });
        }


        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = DeviceInfoUtil.getScreenHeightWithoutStatusBar(context) / 10 * 8;
        } else {
            lp.width = DeviceInfoUtil.getScreenWidth(context) / 10 * 8;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    /**
     * @param context
     * @param title          标题
     * @param des            内容
     * @param middle         中间按钮
     * @param dialogCallBack 点击回调
     * @return
     */
    protected static Dialog showDialog(Context context, String title, String des, String middle, final CustomDialogSingle dialogCallBack) {
        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCancelable(false);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_custom, null);
        dialog.setContentView(view);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        if (!StringUtil.isEmpty(title)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        TextView tvDes = view.findViewById(R.id.tv_des);
        if (!StringUtil.isEmpty(des)) {
            tvDes.setVisibility(View.VISIBLE);
            tvDes.setText(des);
        }

        TextView tvMiddle = view.findViewById(R.id.tv_middle);
        if (!StringUtil.isEmpty(middle)) {
            tvMiddle.setVisibility(View.VISIBLE);
            tvMiddle.setText(middle);
            tvMiddle.setOnClickListener(new OnClickLimitListener() {
                @Override
                public void onClickLimit(View v) {
                    dialog.dismiss();
                    if (dialogCallBack != null) {
                        dialogCallBack.confirm();
                    }

                }
            });
        }
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = DeviceInfoUtil.getScreenHeightWithoutStatusBar(context) / 10 * 8;
        } else {
            lp.width = DeviceInfoUtil.getScreenWidth(context) / 10 * 8;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }

    /***
     * dialog有两个按钮点击
     */
    public interface CustomDialogDouble {
        void rightButtonClicked();

        void leftButtonClicked();
    }

    /**
     * dialog只有一个按钮点击
     */
    public interface CustomDialogSingle {
        void confirm();
    }

}
